using System;
using Csla;
using ENT.PM.Datos.MysqlHelper;
using System.Data;
using MySql.Data.MySqlClient;

namespace ENT.PM.Core
{
    [Serializable]
    public class Usuarios : BusinessBase<Usuarios>
    {
        #region Business Methods

        private static PropertyInfo<int> IdRolProperty = RegisterProperty<int>(o => o.IdRol, "Name");
        public int IdRol
        {
            get { return GetProperty(IdRolProperty); }
            set { SetProperty(IdRolProperty, value); }
        }

        private static PropertyInfo<String> UserNombreProperty = RegisterProperty<String>(o => o.UserNombre, "Name");
        public String UserNombre
        {
            get { return GetProperty(UserNombreProperty); }
            set { SetProperty(UserNombreProperty, value); }
        }

        private static PropertyInfo<String> UserApellidoProperty = RegisterProperty<String>(o => o.UserApellido, "Name");
        public String UserApellido
        {
            get { return GetProperty(UserApellidoProperty); }
            set { SetProperty(UserApellidoProperty, value); }
        }

        private static PropertyInfo<String> UsernameProperty = RegisterProperty<String>(o => o.Username, "Name");
        public String Username
        {
            get { return GetProperty(UsernameProperty); }
            set { SetProperty(UsernameProperty, value); }
        }

        private static PropertyInfo<String> UserClaveProperty = RegisterProperty<String>(o => o.UserClave, "Name");
        public String UserClave
        {
            get { return GetProperty(UserClaveProperty); }
            set { SetProperty(UserClaveProperty, value); }
        }

        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(RuleMethod, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("Name", "Role");
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowEdit(typeof(Usuarios), "Role");
        }

        #endregion

        #region Factory Methods

        public static Usuarios NuevoUsuario()
        {
            return DataPortal.Create<Usuarios>();
        }

        public static Usuarios GetEditableRoot(int id)
        {
            return DataPortal.Fetch<Usuarios>(
              new SingleCriteria<Usuarios, int>(id));
        }

        public static void DeleteEditableRoot(int id)
        {
            DataPortal.Delete(new SingleCriteria<Usuarios, int>(id));
        }

        private Usuarios()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(SingleCriteria<Usuarios, int> criteria)
        {
            // TODO: load values
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            HelperMySql objMysqlHelper = HelperMySql.ObtenerConexion("");
            MySqlParameter[] ojbParametros = new MySqlParameter[5];
            ojbParametros[0] = new MySqlParameter("USER_CLAVE", MySqlDbType.Text);
            ojbParametros[1] = new MySqlParameter("USER_USUARIO", MySqlDbType.Text);
            ojbParametros[2] = new MySqlParameter("USER_APELLIDO", MySqlDbType.Text);
            ojbParametros[3] = new MySqlParameter("USER_NOMBRE", MySqlDbType.Text);
            ojbParametros[4] = new MySqlParameter("ROL_ID", MySqlDbType.Int32);
            ojbParametros[4].Direction = ParameterDirection.Output;

            ojbParametros[0].Value = ReadProperty(UserClaveProperty);
            ojbParametros[1].Value = ReadProperty(UsernameProperty);
            ojbParametros[2].Value = ReadProperty(UserApellidoProperty);
            ojbParametros[3].Value = ReadProperty(UserNombreProperty);
            ojbParametros[4].Value = ReadProperty(IdRolProperty);

            objMysqlHelper.EjecutarStoreProcedure("Insertar_Usuario", ojbParametros);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            // TODO: update values
        }

        //[Transactional(TransactionalTypes.TransactionScope)]
        //protected override void DataPortal_DeleteSelf()
        //{
        //    DataPortal_Delete(new SingleCriteria<Usuarios, int>(this.Id));
        //}

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(SingleCriteria<Usuarios, int> criteria)
        {
            // TODO: delete values
        }

        #endregion
    }
}
